package com.sunny.core.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunny.core.bean.UserInfo;
import com.sunny.core.service.IUserInfoService;

@Controller
@RequestMapping("/user")
public class UserController {
		@Autowired
		private IUserInfoService userInfoService;

		// 用户查询
		@SuppressWarnings("unchecked")
		@RequestMapping(value = "/query", method = { RequestMethod.POST,
				RequestMethod.GET })
		public ModelAndView queryAll(HttpServletRequest request,
				HttpServletResponse response) throws Exception {

			String pageNum = request.getParameter("pageNum");
			String pageSize = request.getParameter("pageSize");
			int num = 1;
			int size = 2;
			if (pageNum != null && !"".equals(pageNum)) {
				num = Integer.parseInt(pageNum);
			}
			if (pageSize != null && !"".equals(pageSize)) {
				size = Integer.parseInt(pageSize);
			}
			String sortString = "id.desc";
			Order.formString(sortString);

			PageHelper.startPage(num, size);
			List<UserInfo> userInfoList = userInfoService.queryAll();

			PageInfo<UserInfo> pagehelper = new PageInfo<UserInfo>(userInfoList);

			// 返回ModelAndView
			ModelAndView modelAndView = new ModelAndView();
			// 相当 于request的setAttribut，在jsp页面中通过personList取数据
			// modelAndView.addObject("personList", personList);
			modelAndView.addObject("pagehelper", pagehelper);

			// 指定视图
			// 下边的路径，如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀，修改为
			// modelAndView.setViewName("/jsp/personList.jsp");
			// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
			modelAndView.setViewName("/userInfoList");
			return modelAndView;

		}
}
